package com.hezhanying.controller;

import com.hezhanying.pojo.Emp;
import com.hezhanying.pojo.PageBean;
import com.hezhanying.pojo.Result;
import com.hezhanying.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@Slf4j
@RestController
@RequestMapping("/api/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping
    public Result getAllEmp(
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize,
            String name,
            Short gender,
            @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
            @DateTimeFormat(pattern = "yyyy-MM-dd")LocalDate end){
        log.info("分页查询，参数：{},{},{},{},{},{}", pageNo, pageSize,name, gender, begin, end);
        PageBean pageBean= empService.getAllEmp(pageNo,pageSize,name,gender,begin,end);
        return Result.success(pageBean);
    }
    @PostMapping
    public Result addEmp(@RequestBody Emp emp){
        log.info("添加员工信息");
        empService.addEmp(emp);
        return Result.success();
    }
    @DeleteMapping("/{id}")
    public Result deleteEmp(@PathVariable("id") Integer id){
        log.info("删除员工信息");
        empService.deleteEmp(id);
        return Result.success();
    }
    @PutMapping
    public Result updateEmp(@RequestBody Emp emp){
        log.info("更新员工信息");
        empService.updateEmp(emp);
        return Result.success();
    }
}
